
//
//  VisitorLoginView.swift
//  weibo
//
//  Created by Fosen on 15/11/27.
//  Copyright © 2015年 fosen. All rights reserved.
//

import UIKit

protocol VisitorLoginViewDelegate:NSObjectProtocol{

//协议提供方法
    
    //注册协议方法
    func userWillLogin()
    func userwillRegister()

}

class VisitorLoginView: UIView {

    
    //提供代理对象
    //在swift中所有的属性 默认都是strong
    weak var visitorLoginViewDelegate: VisitorLoginViewDelegate?
    
    //对外提供设置访客视图的图片和信息
    func setWithInfo(messageText:String ,imageName:String?){
        
        messageLabel.text = messageText
        if let imageName = imageName{
            circleView.image = UIImage(named: imageName)
            iconView.hidden = true
            bringSubviewToFront(circleView)
        }else{
        
            //添加动画
            startAnimation()
            
        }
    }
    
    private func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.duration = 10.0
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false  //完成动画 当视图处于非活跃状态  移除图层
        circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //在设置frame的时候 添加子控件
    private func setupUI(){
    
        addSubview(circleView)
        addSubview(backView)

        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置布局
        //VFL 进行自动布局  
        //使用VFL手动添加约束  必须将frame设置试图位置的属性关闭
        
        //<#T##AnyObject#> 添加约束的对象
//        circleView.translatesAutoresizingMaskIntoConstraints = false
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        messageLabel.translatesAutoresizingMaskIntoConstraints = false

        for view in subviews{
        
            view.translatesAutoresizingMaskIntoConstraints = false
        
        }
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -50))
        
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 26))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //oc中有多个枚举选项 使用按位或   在swift中 使用数组
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options: [], metrics: nil, views: ["maskView":backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-(-35)-[regBtn]", options: [], metrics: nil, views: ["maskView":backView,"regBtn":registerBtn]))

        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
        
        
        loginBtn.addTarget(self, action: "loginDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerDidClick", forControlEvents: .TouchUpInside)
        

    }
    
    //响应方法
    @objc private func loginDidClick(){
        visitorLoginViewDelegate?.userWillLogin()

    }

    @objc private func registerDidClick(){
        visitorLoginViewDelegate?.userwillRegister()
    
    }
    
    
    //懒加载子视图控件 
    
    private lazy var backView :UIImageView = UIImageView(image: UIImage (named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var circleView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
    private lazy var messageLabel :UILabel =  UILabel(title: "关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
        


    private lazy var loginBtn :UIButton = UIButton(title: "登录", backImage: "common_button_white_disable", color: UIColor.darkGrayColor(),fontSize: 10,imageName: nil)

    
    private lazy var registerBtn :UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color: UIColor.orangeColor(),fontSize: 10,imageName: nil)
    
  

    
}
